package com.tcredit.sdk.android;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.DragEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroupOverlay;
import android.view.ViewParent;
import android.webkit.WebView;

public class MyWebView extends WebView {


    private int newdeltaY;
    private int olddeltaY;
    public ScrollInterface mScrollInterface;
    public MyWebView (Context context) {
        super(context);
    }
    public MyWebView (Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    public MyWebView (Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    //实时滑动监控
    //参数l代表滑动后当前位置，old代表原来原值
    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
       // LogUtil.e("--onScrollChanged--"+l+"---"+t+"---"+oldl+"---"+oldt);
        mScrollInterface.onSChanged(l, t, oldl, oldt);


    }


    @Override
    protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
        super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
       // LogUtil.e("onOverScrolled---"+"-scrollY--"+scrollY+"--clampedX--"+clampedX+"--clampedY--"+clampedY);
       // mScrollInterface1.onSChanged(scrollX,scrollY,clampedX,clampedY);
    }




    //当webview内有弹框的时候是不会走这个方法的，只有在弹窗之外才走这个方法！！！
    //deltaY会有等于0的时候
    @Override
    protected boolean overScrollBy(int deltaX, int deltaY, int scrollX,
                                   int scrollY, int scrollRangeX, int scrollRangeY,
                                   int maxOverScrollX, int maxOverScrollY,
                                   boolean isTouchEvent) {
        newdeltaY=deltaY;
       // LogUtil.e("8888--"+"deltaY："+deltaY+"  scrollY："+scrollY+"  scrollRangeY："+scrollRangeY+"  maxOverScrollY："+maxOverScrollY+"  isTouchEvent"+isTouchEvent);
        return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, maxOverScrollX, maxOverScrollY, isTouchEvent);
       // return true;
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if(newdeltaY!=olddeltaY){
            olddeltaY=newdeltaY;
            ViewParent parent = getParent();
            if (parent instanceof SpringView){
                ((SpringView) parent).setEnable(true);
            }
        }else {
            ViewParent parent = getParent();
            if (parent instanceof SpringView){
                ((SpringView) parent).setEnable(false);
            }
        }
        return super.onTouchEvent(event);
    }

    //供外部调用，监控滑动
    public void setOnCustomScroolChangeListener(ScrollInterface scrollInterface) {
        this.mScrollInterface = scrollInterface;
    }
    public interface ScrollInterface {
        public void onSChanged(int l, int t, int oldl, int oldt);
    }



}

